package com.xiaok.netty.server.service.impl;

import com.xiaok.netty.server.service.MonitorService;
import com.xiaok.netty.server.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Classname MonitorServiceImpl
 * @Description TODO
 * @Date 2020/9/29 20:07
 * @Created by xiaok
 */
@Service
public class MonitorServiceImpl implements MonitorService {

    private final String pattern = "heart:monitor:*";
    @Autowired
    RedisUtils redisUtils;

    /**
     * 获取监控的数据
     * @return
     */
    @Override
    public Map<String, Object> getData() {

        Map<String,Object> map = new HashMap<>();
        Set<Object> keys = redisUtils.keys(pattern);
        for(Object  k : keys){
            String s = String.valueOf(k);
            List<Object> pingList = redisUtils.rangeByList(s, 0l, 9l);
            if(!pingList.isEmpty()){
                Collections.reverse(pingList);
                int index = s.lastIndexOf(":");
                map.put(s.substring(index+1),pingList);
            }

        }

        return map;
    }
}
